﻿using System;
using SmartSiteManagementSystem.IRepository.ProjectManage;
using SmartSiteManagementSystem.Models.ProjectManage;
using SmartSiteManagementSystem.Repository.DBHelper;
using System.Data;
using Dapper;
using System.Collections;
using System.Collections.Generic;
using System.Text;
using System.Linq;
using MySql.Data.MySqlClient;

namespace SmartSiteManagementSystem.Repository.ProjectManage
{
    public class ProjectInfoRepository : IProjectInfoRepository
    {
        #region dataTable
        /*
drop table if exists `project`;
create table `project`( 
	`project_id` int(11) not null AUTO_INCREMENT primary key, 
	`project_name` nvarchar(250) NOT NULL,
    `project_type` tinyint NOT NULL default 0,
    `start_time` datetime not null default now(),
    `end_time` datetime not null default now(),
    `is_delete` bit NOT NULL default 0,
	`create_time` datetime not null default now(),
	`update_time` datetime not null default now()
) default charset=utf8;
         */
        #endregion
        //private IDbConnection _readConn;
       // private IDbConnection _writeConn;

        public bool Delete(Hashtable param)
        {
            bool back = false;
            if (param == null)
                param = new Hashtable();
            String querySql = @"delete from project";
            using (IDbConnection _writeConn = new MySqlConnection(DbHelperOptions.WriteConnectionString))
            {

                DynamicParameters pars = new DynamicParameters();
                StringBuilder whereCondition = new StringBuilder();
                if (param.Count > 0)
                {
                    whereCondition.Append(" where ");
                    int parCount = 0;
                    if (param.ContainsKey("project_id"))
                    {
                        pars.Add("project_id", param["project_id"]);

                        whereCondition.Append(" project_id =@project_id");
                        parCount++;
                    }
                }
                string execSql = querySql + whereCondition.ToString();
                int backInt = _writeConn.Execute(execSql, pars);
                if (backInt > 0)
                {
                    back = true;
                }
            }
            return back;
        }

        public ProjectInfo Get(Hashtable param)
        {
            ProjectInfo back = null;
            if (param == null)
                param = new Hashtable();
            String querySql = @"SELECT project_id as ProjectId,project_name as ProjectName,project_type as ProjectType,start_time as StartTime,end_time as EndTime,is_delete as IsDelete,create_time as CreateTime,update_time as UpdateTime FROM project";
            using (IDbConnection _readConn = new MySqlConnection(DbHelperOptions.ReadConnectionString))
            {

                DynamicParameters pars = new DynamicParameters();
                StringBuilder whereCondition = new StringBuilder();
                if (param.Count > 0)
                {
                    whereCondition.Append(" where 1=1");
                    if (param.ContainsKey("project_id"))
                    {
                        pars.Add("project_id", param["project_id"]);
                        whereCondition.Append(" and project_id =@project_id");
                    }
                    if (param.ContainsKey("project_name"))
                    {
                        pars.Add("project_name", param["project_name"]);
                        whereCondition.Append(" and project_name =@project_name");
                    }
                    if (param.ContainsKey("day_project_type"))
                    {
                        pars.Add("project_type", param["project_type"]);
                        whereCondition.Append(" and project_type =@project_type");
                    }
                  
                    if (param.ContainsKey("start_time"))
                    {
                        pars.Add("start_time", param["start_time"]);
                        whereCondition.Append(" and start_time >=@start_time");
                    }

                    if (param.ContainsKey("end_time"))
                    {
                        pars.Add("end_time", param["end_time"]);
                        whereCondition.Append(" and end_time <=@end_time");
                    }

                   
                    if (param.ContainsKey("is_delete"))
                    {
                        pars.Add("is_delete", param["is_delete"]);
                        whereCondition.Append(" and is_delete =@is_delete");
                    }
                }
                whereCondition.Append(" limit 1");
                string execSql = querySql + whereCondition.ToString();
                back = _readConn.QueryFirstOrDefault<ProjectInfo>(execSql, pars);
            }
            return back;
        }

        public int GetCount(Hashtable param)
        {
            int back = 0;
            if (param == null)
                param = new Hashtable();
            String querySql = "select count(project_id) from project";
            using (IDbConnection _readConn = new MySqlConnection(DbHelperOptions.ReadConnectionString))
            {
                #region
                DynamicParameters pars = new DynamicParameters();
                StringBuilder whereCondition = new StringBuilder();
                if (param.Count > 0)
                {
                    whereCondition.Append(" where 1=1");
                    if (param.ContainsKey("project_id"))
                    {
                        pars.Add("project_id", param["project_id"]);
                        whereCondition.Append(" and project_id =@project_id");
                    }
                    if (param.ContainsKey("project_name"))
                    {
                        pars.Add("project_name", param["project_name"]);
                        whereCondition.Append(" and project_name =@project_name");
                    }
                    if (param.ContainsKey("day_project_type"))
                    {
                        pars.Add("project_type", param["project_type"]);
                        whereCondition.Append(" and project_type =@project_type");
                    }

                    if (param.ContainsKey("start_time"))
                    {
                        pars.Add("start_time", param["start_time"]);
                        whereCondition.Append(" and start_time >=@start_time");
                    }

                    if (param.ContainsKey("end_time"))
                    {
                        pars.Add("end_time", param["end_time"]);
                        whereCondition.Append(" and end_time <=@end_time");
                    }


                    if (param.ContainsKey("is_delete"))
                    {
                        pars.Add("is_delete", param["is_delete"]);
                        whereCondition.Append(" and is_delete =@is_delete");
                    }
                }

                string execSql = querySql + whereCondition.ToString();
                #endregion
                back = _readConn.QueryFirstOrDefault<int>(execSql, pars);
            }
            return back;
        }

        public IList<ProjectInfo> GetList(Hashtable param, string orderBy, int first, int rows)
        {

            IList<ProjectInfo> back = null;
            if (param == null)
                param = new Hashtable();
            String querySql = @"SELECT project_id as ProjectId,project_name as ProjectName,project_type as ProjectType,start_time as StartTime,end_time as EndTime,is_delete as IsDelete,create_time as CreateTime,update_time as UpdateTime FROM project";
            using (IDbConnection _readConn = new MySqlConnection(DbHelperOptions.ReadConnectionString))
            {
                #region
                DynamicParameters pars = new DynamicParameters();
                StringBuilder whereCondition = new StringBuilder();
                if (param.Count > 0)
                {
                    whereCondition.Append(" where 1=1");
                    if (param.ContainsKey("project_id"))
                    {
                        pars.Add("project_id", param["project_id"]);
                        whereCondition.Append(" and project_id =@project_id");
                    }
                    if (param.ContainsKey("project_name"))
                    {
                        pars.Add("project_name", param["project_name"]);
                        whereCondition.Append(" and project_name =@project_name");
                    }
                    if (param.ContainsKey("day_project_type"))
                    {
                        pars.Add("project_type", param["project_type"]);
                        whereCondition.Append(" and project_type =@project_type");
                    }

                    if (param.ContainsKey("start_time"))
                    {
                        pars.Add("start_time", param["start_time"]);
                        whereCondition.Append(" and start_time >=@start_time");
                    }

                    if (param.ContainsKey("end_time"))
                    {
                        pars.Add("end_time", param["end_time"]);
                        whereCondition.Append(" and end_time <=@end_time");
                    }


                    if (param.ContainsKey("is_delete"))
                    {
                        pars.Add("is_delete", param["is_delete"]);
                        whereCondition.Append(" and is_delete =@is_delete");
                    }
                }
                if (string.IsNullOrEmpty(orderBy))
                {
                    whereCondition.Append(" order by project_id desc");
                }
                else
                {
                    whereCondition.Append(" order by @orderBy");
                    pars.Add("orderBy", orderBy);
                }
                whereCondition.Append(" limit @first,@rows");
                pars.Add("first", first);
                pars.Add("rows", rows);
                #endregion
                string execSql = querySql + whereCondition.ToString();
                back = _readConn.Query<ProjectInfo>(execSql, pars).AsList<ProjectInfo>();
            }
            return back;
        }

        public int Insert(ProjectInfo obj)
        {
            int back = 0;
            #region sql
            string sql = @"INSERT INTO project
(
	project_name,
    project_type,
    start_time,
    end_time,
    is_delete,
	create_time,
	update_time
)
VALUES
(
	@project_name,
    @project_type,
    @start_time,
    @end_time,
    @is_delete,
	@create_time,
	@update_time);select last_insert_id();";
            #endregion
            using (IDbConnection _writeConn = new MySqlConnection(DbHelperOptions.WriteConnectionString))
            {

                back = _writeConn.QueryFirst<int>(sql, new
                {
                    project_name = obj.ProjectName,
                    project_type = obj.ProjectType,
                    start_time = obj.StartTime,
                    end_time = obj.EndTime,
                    is_delete = obj.IsDelete,
                    create_time = obj.CreateTime,
                    update_time = obj.UpdateTime
                });
            }
            return back;
        }

        public bool Update(ProjectInfo obj)
        {
            bool back = false;
            #region sql
            string sql = @"update project set
	project_name=@project_name,
    project_type=@project_type,
    start_time=@start_time,
    end_time=@end_time,
    is_delete=@is_delete,
	create_time=@create_time,
	update_time=@update_time
WHERE project_id =@project_id;";
            #endregion
            using (IDbConnection _writeConn = new MySqlConnection(DbHelperOptions.WriteConnectionString))
            {

                int backint = _writeConn.Execute(sql, new
                {
                    project_id=obj.ProjectId,
                    project_name = obj.ProjectName,
                    project_type = obj.ProjectType,
                    start_time = obj.StartTime,
                    end_time = obj.EndTime,
                    is_delete = obj.IsDelete,
                    create_time = obj.CreateTime,
                    update_time = obj.UpdateTime
                });
                if (backint > 0)
                {
                    back = true;
                }
            }
            return back;
        }
    }
}

